//
//  RegisteredViewController.m
//  HandheldHospital
//
//  Created by zhangchao on 16/3/2.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "RegisteredViewController.h"
#import "MyView.h"
#import "DataManager.h"
#import "PatientData.h" 
#import "Registering.h"
#import <Masonry.h>
#import "AddManagerViewController.h"
#import "MyScrollView.h"

#define kShared [DataManager defaultManager]
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
#define kViewHeight 50
#define kLabelHeight 31
#define kLabelTop 9
#define kLabelleft 10

@interface RegisteredViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
/**
 *  医院
 */
@property (nonatomic,strong) UILabel *hospitalLabel;
/**
 *  科室
 */
@property (nonatomic,strong) UITextField *departmentsTF;
/**
 *  挂号费
 */
@property (nonatomic,strong) UILabel *costLabel;
/**
 *  日期
 */
@property (nonatomic,strong) UITextField *registerDateTF;
/**
 *  挂号时间
 */
@property (nonatomic,strong) UITextField *registerTimeTF;
/**
 *  就诊人
 */
@property (nonatomic,strong) UITextField *patientTF;
@property (nonatomic,strong) PatientData *patient;
/**
 *  科室数组
 */
@property (nonatomic,strong) NSArray *departmentsArray;
@property (nonatomic,strong) MyScrollView *backGroudView;
@end

@implementation RegisteredViewController

- (NSArray *)departmentsArray
{
    if(_departmentsArray == nil)
    {
        _departmentsArray = [NSArray arrayWithObjects:@"急诊科",@"儿科",@"眼科",@"口腔科",@"皮肤性病科",@"妇科",@"产科",@"内分泌科",@"骨科",@"肝胆外科",@"泌尿外科",@"耳鼻喉科",@"头颈外科",@"心血管内科",@"神经内科",@"神经外科",@"肛肠外科",@"乳腺甲状腺外科",@"整形、激光美容外科",@"心胸外科",@"消化内科",@"呼吸内科",@"中西医结合科",@"血液、肿瘤科",@"肾内科",@"放射科",@"肠道科",@"药剂科", nil];
    }
    return _departmentsArray;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.patientTF.enabled = NO;
    UIPickerView *picker = [self.view viewWithTag:604];
    [kShared getDataArrayAndBlock:^{
        [picker reloadAllComponents];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"预约确定";
    self.view.backgroundColor = [UIColor whiteColor];
    //简单布局
    [self initlayout];

    // Do any additional setup after loading the view.
}

- (void)initlayout
{
    
    self.backGroudView = [[MyScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.backGroudView.contentSize = CGSizeMake(kWidth , kHeight * 1.3);
    //底色
    self.backGroudView.backgroundColor = [UIColor brownColor];
    self.backGroudView.alpha = 0.5;
    [self.view addSubview:self.backGroudView];
    __block typeof(self) myself = self;
    self.backGroudView.touchBlock = ^
    {
        [myself.registerDateTF resignFirstResponder];
        [myself.registerTimeTF resignFirstResponder];
        [myself.patientTF resignFirstResponder];
        [myself.departmentsTF resignFirstResponder];
    };
    
    
    UIView *HospitalView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(self.view.bounds), 50)];
    HospitalView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:HospitalView];
    
    MyView *DepartmentView = [[MyView alloc] initWithFrame:CGRectMake(0, 101, CGRectGetWidth(self.view.bounds), 50)];
    DepartmentView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:DepartmentView];
    
    UIView *DatatimeView = [[UIView alloc] initWithFrame:CGRectMake(0, 152, CGRectGetWidth(self.view.bounds), 50)];
    DatatimeView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:DatatimeView];
    
    MyView *InformationView = [[MyView alloc] initWithFrame:CGRectMake(0, 240, CGRectGetWidth(self.view.bounds), 50)];
    InformationView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:InformationView];
    
    MyView *VisitView = [[MyView alloc] initWithFrame:CGRectMake(0, 291, CGRectGetWidth(self.view.bounds), 50)];
    VisitView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:VisitView];
    
    MyView *PeopleView = [[MyView alloc] initWithFrame:CGRectMake(0, 380, CGRectGetWidth(self.view.bounds), 50)];
    PeopleView.backgroundColor = [UIColor whiteColor];
    [self.backGroudView addSubview:PeopleView];
    
    UIButton *submit = [UIButton buttonWithType:UIButtonTypeSystem];
//    Submit.frame = CGRectMake(10, 540, 355, 51);
    submit.tintColor = [UIColor whiteColor];
    submit.backgroundColor = [UIColor blueColor];
    submit.layer.cornerRadius = 20.0;
    [submit setTitle:@"提交" forState:UIControlStateNormal];
    [submit addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchUpInside];
    [self.backGroudView addSubview:submit];
    [submit mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (480);
        make.left.offset (kLabelleft);
        make.size.mas_equalTo (CGSizeMake(kWidth - 20, 51));
    }];
    
    //小布局
    UILabel *BookingInformation = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 70, 31)];
    BookingInformation.text = @"预约信息";
    BookingInformation.font = [UIFont systemFontOfSize:15];
    [self.backGroudView addSubview:BookingInformation];
    
    UILabel *BookingTime = [[UILabel alloc] initWithFrame:CGRectMake(10, 210, 70, 31)];
    BookingTime.text = @"预约时间";
    BookingTime.font = [UIFont systemFontOfSize:15];
    [self.backGroudView addSubview:BookingTime];
    
    UILabel *MedicaIinformation = [[UILabel alloc] initWithFrame:CGRectMake(10, 350, 70, 31)];
    MedicaIinformation.text = @"就诊信息";
    MedicaIinformation.font = [UIFont systemFontOfSize:15];
    [self.backGroudView addSubview:MedicaIinformation];
    
    //预约信息 预约时间 就诊信息
    /*  医院  */
    
    UILabel *hospital = [[UILabel alloc] init];
    hospital.text = @"医院";
    [HospitalView addSubview:hospital];
    [hospital mas_makeConstraints:^(MASConstraintMaker *make)
     {
         
         make.top.offset (kLabelTop);
         make.left.offset (kLabelleft);
         make.size.mas_equalTo (CGSizeMake(50, kLabelHeight));
         
     }];
    
    
    self.hospitalLabel = [[UILabel alloc] init];
    self.hospitalLabel.text = self.hospitalName;
    self.hospitalLabel.textAlignment = NSTextAlignmentRight;
    [HospitalView addSubview:self.hospitalLabel];
    [self.hospitalLabel mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.offset (kLabelTop);
         make.left.offset (kWidth - 145);
         make.size.mas_equalTo (CGSizeMake(140, kLabelHeight));
         
     }];

    /*  科室  */
    UILabel *department = [[UILabel alloc] init];
    department.text = @"科室";
    [DepartmentView addSubview:department];
    [department mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (kLabelTop);
        make.left.offset (kLabelleft);
        make.size.mas_equalTo (CGSizeMake(50, kLabelHeight));
        
    }];

    UIPickerView *picker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    picker1.delegate = self;
    picker1.tag = 601;
    
    self.departmentsTF = [[UITextField alloc] init];
    self.departmentsTF.textAlignment = NSTextAlignmentRight;
    self.departmentsTF.placeholder = @"请选择科室";
    self.departmentsTF.textColor = [UIColor blackColor];
    self.departmentsTF.inputView = picker1;
    [DepartmentView addSubview:self.departmentsTF];
    [self.departmentsTF mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (kLabelTop);
        make.left.offset (kWidth - 105);
        make.size.mas_equalTo (CGSizeMake(100, kLabelHeight));
        
    }];
    
    /*  挂号费  */
    UILabel *money = [[UILabel alloc] init];
    money.text = @"挂号费";
    [DatatimeView addSubview:money];
    [money mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (kLabelTop);
        make.left.offset (kLabelleft);
        make.size.mas_equalTo (CGSizeMake(60, kLabelHeight));
    }];

    self.costLabel = [[UILabel alloc] init];
    self.costLabel.textAlignment = NSTextAlignmentRight;
    self.costLabel.text = @"￥0.01";
    [DatatimeView addSubview:self.costLabel];
    [self.costLabel mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (kLabelTop);
        make.left.offset (kWidth - 75);
        make.size.mas_equalTo (CGSizeMake(70, kLabelHeight));
    }];
    
    /*   日期  */
    UILabel *data = [[UILabel alloc] init];
    data.text = @"日期";
    [InformationView addSubview:data];
    [data mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (kLabelTop);
        make.left.offset (kLabelleft);
        make.size.mas_equalTo (CGSizeMake(60, kLabelHeight));
    }];
    
    
    UIDatePicker *datePick = [[UIDatePicker alloc] init];
    [datePick setDatePickerMode:UIDatePickerModeDate];
    datePick.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CH"];
    [datePick addTarget:self action:@selector(dateChage:) forControlEvents:UIControlEventValueChanged];
    datePick.tag = 602;

    self.registerDateTF = [[UITextField alloc] init];
    self.registerDateTF.textAlignment = NSTextAlignmentRight;
    self.registerDateTF.placeholder = @"请输入日期";
    self.registerDateTF.textColor = [UIColor blackColor];
    self.registerDateTF.inputView = datePick;
    [InformationView addSubview:self.registerDateTF];
    [self.registerDateTF mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (kLabelTop);
        make.left.offset (kWidth - 145);
        make.size.mas_equalTo (CGSizeMake(140, kLabelHeight));
    }];

    /*  坐诊时间  */
    UILabel *visitTimeOny = [[UILabel alloc] init];
    visitTimeOny.text = @"坐诊时间";
    [VisitView addSubview:visitTimeOny];
    [visitTimeOny mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset(kLabelTop);
        make.left.offset(kLabelleft);
        make.size.mas_equalTo(CGSizeMake(70, kLabelHeight));
    }];
    
    UIPickerView *picker2 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    picker2.tag = 603;
    picker2.delegate = self;
    
    self.registerTimeTF = [[UITextField alloc] init];
    self.registerTimeTF.placeholder = @"上午08:20-11:30";
    self.registerTimeTF.textAlignment = NSTextAlignmentRight;
    self.registerTimeTF.inputView = picker2;
    [VisitView addSubview:self.registerTimeTF];
    [self.registerTimeTF mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (kLabelTop);
        make.left.offset (kWidth - 145);
        make.size.mas_equalTo (CGSizeMake(140, kLabelHeight));
    }];
    
    /*  就诊信息  */

    UILabel *peopleOny = [[UILabel alloc] init];
    peopleOny.text = @"就诊人";
    [PeopleView addSubview:peopleOny];
    [peopleOny mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (kLabelTop);
        make.left.offset (kLabelleft);
        make.size.mas_equalTo (CGSizeMake(70, kLabelHeight));
    }];
    
    UIPickerView *picker3 = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    picker3.tag = 604;
    picker3.delegate = self;
    

    self.patientTF = [[UITextField alloc] init];
    self.patientTF.placeholder = @"请选择就诊人";
    self.patientTF.textAlignment = NSTextAlignmentRight;
    self.patientTF.inputView = picker3;
    [PeopleView addSubview:self.patientTF];
    self.patientTF.enabled = NO;
    [self.patientTF mas_makeConstraints:^(MASConstraintMaker *make)
    {
        make.top.offset (kLabelTop);
        make.left.offset (kWidth - 145);
        make.size.mas_equalTo (CGSizeMake(140, kLabelHeight));
    }];
    
    //科室
    DepartmentView.block = ^
    {
        [self.departmentsTF becomeFirstResponder];
    };
    //日期
    InformationView.block = ^
    {
        [self.registerDateTF becomeFirstResponder];
    };
    //坐诊时间
    VisitView.block = ^
    {
        [self.registerTimeTF becomeFirstResponder];
    };
    //就诊人
    PeopleView.block = ^
    {
        //先判断有没有就诊人，如果没有先添加就诊人
        if(!(kShared.dataArray.count > 0))
        {
            [self showAlertTwoButtonWithTitle:@"提示" AndMessage:@"请先添加就诊人" AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"确定" AndTwoButtonTitle:@"取消" AndOneBlock:^{
                AddManagerViewController *addManagerVC = [[AddManagerViewController alloc] init];
                [self.navigationController pushViewController:addManagerVC animated:YES];
            } AndTwoBlock:nil];
        }
        else
        {
            self.patientTF.enabled = YES;
            [self.patientTF becomeFirstResponder];
        }
    };
}


#pragma mark   ----- Action

//提交按钮点击事件
- (void)submitAction
{
    if(self.departmentsTF.text.length == 0)
    {
        [self showLabelWithString:@"请先择科室" AndBlock:nil];
    }
    else if(self.registerDateTF.text.length == 0)
    {
        [self showLabelWithString:@"请选择日期" AndBlock:nil];
    }
    else if(self.registerTimeTF.text.length == 0)
    {
        [self showLabelWithString:@"请选择时间" AndBlock:nil];
    }
    else if(self.patientTF.text.length == 0)
    {
        [self showLabelWithString:@"请选择就诊人" AndBlock:nil];
    }
    else
    {
        //将挂订信息保存到数据库中
        Registering *registering = [Registering object];
        registering.currentUser = [AVUser currentUser];
        registering.patient = self.patient;
        registering.hospitalName = self.hospitalLabel.text;
        registering.departments = self.departmentsTF.text;
        registering.cost = self.costLabel.text;
        registering.registerDate = self.registerDateTF.text;
        registering.registerTime = self.registerTimeTF.text;
        registering.registerType = @"未交费";
        [registering saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if(succeeded)
            {
                [self showAlertOneButtonWithTitle:@"提示" AndMessage:@"挂号成功，请去“我的－挂号订单”缴费" AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"确定" AndBlock:^{
                   [self.navigationController popViewControllerAnimated:YES];
                }];
            }
            else
            {
                [self showAlertOneButtonWithTitle:@"错误" AndMessage:@"未知错误，挂号失败" AndAlertStyle:UIAlertControllerStyleAlert AndOneButtonTitle:@"确定" AndBlock:nil];
            }
        }];
    }
}



#pragma mark pickerView代理方法

//设置pickerView组件的个数 （即滚轮个数）
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
//设置组件里的行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(pickerView.tag == 601)
    {
        return self.departmentsArray.count;
        
    }
    if(pickerView.tag == 603)
    {
        return 2;
    }
    if(pickerView.tag == 604)
    {
        return kShared.dataArray.count;
    }
    return 0;
}

//滚轮显示的内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

    if(pickerView.tag == 601)
    {
        self.departmentsTF.text = self.departmentsArray[row];
        return self.departmentsArray[row];
    }
    if(pickerView.tag == 603)
    {
        if(row == 0)
        {
            self.registerTimeTF.text = @"上午08:30-11:30";
            return @"上午08:30-11:30";
        }else if(row == 1)
        {
            self.registerTimeTF.text = @"下午13:30-16:30";
            return @"下午13:30-16:30";
        }
    }
    if(pickerView.tag == 604)
    {
        PatientData *patient = kShared.dataArray[row];
        self.patientTF.text = patient.name;
        self.patient = patient;
        return patient.name;
    }
    return nil;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(pickerView.tag == 601)
    {
        self.departmentsTF.text = self.departmentsArray[row];
    }
    if(pickerView.tag == 603)
    {
        if(row == 0)
        {
            self.registerTimeTF.text = @"上午08:30-11:30";
        }else if(row == 1)
        {
            self.registerTimeTF.text = @"下午13:30-16:30";
        }
    }
    if(pickerView.tag == 604)
    {
        PatientData *patient = kShared.dataArray[row];
        self.patientTF.text = patient.name;
        self.patient = patient;
    }
}

//获取datapicker上显示的时间数据
- (void)dateChage:(UIDatePicker *)picker
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *firstTime = [formatter stringFromDate:picker.date];
    NSString *secondTime = [formatter stringFromDate:[NSDate date]];
    if([firstTime compare:secondTime] < 0)
    {
        [self showLabelWithString:@"请选择正确的预约日期" AndBlock:nil];
    }
    else
    {
        self.registerDateTF.text = [formatter stringFromDate:picker.date];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
